package com.wolfcode.goods.service.impl;

import com.wolfcode.goods.domain.Goods;
import com.wolfcode.goods.domain.info.GoodsInfo;
import com.wolfcode.goods.domain.vo.GoodsSaveVo;
import com.wolfcode.goods.domain.vo.GoodsUpdateVo;
import com.wolfcode.goods.domain.vo.GoodsVo;
import com.wolfcode.goods.mapper.GoodsMapper;
import com.wolfcode.goods.service.IGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;




/**
 * 物品信息Service业务层处理
 *
 * @author wolfcode
 * @date 2022-12-10
 */
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询物品信息
     *
     * @param id 物品信息主键
     * @return 物品信息
     */
    @Override
    public Goods selectGoodsById(Long id) {
        return goodsMapper.selectGoodsById(id);
    }


    /**
     * 查询物品信息列表
     *
     * @param info 物品信息
     * @return 物品信息
     */
    @Override
    public List<GoodsInfo> selectGoodsInfoList(GoodsVo info) {
        // 参数校验
        Assert.notNull(info, "非法参数");
        return goodsMapper.selectGoodsVoList(info);
    }


    /**
     * 新增物品信息
     *
     * @param goodsAndFileVo 物品信息
     * @return 结果
     */
    @Override
    public void insertGoods(GoodsSaveVo goodsAndFileVo) throws Exception {
        Assert.notNull(goodsAndFileVo, "非法参数异常");
        // 获取文件后缀
        String originalFilename = goodsAndFileVo.getGoodsCover();
        String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        if (!(extension.equals("pnj") || extension.equals("jpg"))) {
            throw new Exception("不是图片格式");
        }
        Assert.hasLength(goodsAndFileVo.getGoodsName(), "名称不能为空");
        Assert.notNull(goodsAndFileVo.getCategoryId(), "分类不能为空");
        Assert.state(goodsAndFileVo.getGoodsName().length() < 50, "物品名称不能超过50字");
        Assert.state(goodsAndFileVo.getBrand().length() < 50, "品牌名称不能超过50字");
        Assert.state(goodsAndFileVo.getGoodsDesc().length() < 200, "描述不能超过200字");
        //存储图片的路径
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsAndFileVo, goods);
        goodsMapper.insertGoods(goods);
    }

    /**
     * 修改物品信息
     *
     * @param goodsAndFileVo 物品信息
     * @return 结果
     */
    @Override
    public void updateGoods(GoodsUpdateVo goodsAndFileVo) throws Exception {
        Assert.notNull(goodsAndFileVo, "非法参数异常");
        // 获取文件后缀
        String originalFilename = goodsAndFileVo.getGoodsCover();
        String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        if (!(extension.equals("pnj") || extension.equals("jpg"))) {
            throw new Exception("不是图片格式");
        }
        Assert.hasLength(goodsAndFileVo.getGoodsName(), "名称不能为空");
        Assert.notNull(goodsAndFileVo.getCategoryId(), "分类不能为空");
        Assert.state(goodsAndFileVo.getGoodsName().length() < 50, "物品名称不能超过50字");
        Assert.state(goodsAndFileVo.getBrand().length() < 50, "品牌名称不能超过50字");
        Assert.state(goodsAndFileVo.getGoodsDesc().length() < 200, "描述不能超过200字");
        //存储图片的路径
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsAndFileVo, goods);
        System.out.println(goods);
        goodsMapper.updateGoods(goods);
    }


    /**
     * 批量删除物品信息
     *
     * @param ids 需要删除的物品信息主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {
        return goodsMapper.deleteGoodsByIds(ids);

    }

    /**
     * 删除物品信息信息
     *
     * @param id 物品信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteGoodsById(Long id)
    {
        Assert.notNull(id,"非法参数");
        Goods goods = goodsMapper.selectGoodsById(id);
        Assert.notNull(goods,"，没有物品信息");
        goodsMapper.deleteGoodsSoreBy(id);
        return goodsMapper.deleteGoodsById(id);

    }


}
